import { defineStore } from 'pinia'
import { useLoginStore } from './modules/login'
import { getPageListData } from '@/service/main/system'
export function setupUserStore() {
  // 页面初始调用方法
  useLoginStore().loadLocaLogin()
  // 直接调用
  useInitialStore().getInitialDataAction()
}

// entire - 全部的
interface InitialType {
  entireDepartment: any[]
  entireRole: any[]
  entireMenu: any[]
}
// 初始数据  一开始就从服务器拿到，不跟随页面变化才发请求获取
export const useInitialStore = defineStore('Initial', {
  state: (): InitialType => ({
    entireDepartment: [],
    entireRole: [],
    entireMenu: []
  }),
  actions: {
    // 获取初始的信息 不需要参数直接写死获取 固定url
    async getInitialDataAction() {
      // 获取数据 部门和角色
      const departmentResult = await getPageListData('/department/list', {
        offset: 0,
        size: 1000
      })
      const { list: departmentList } = departmentResult.data
      // 一个错误：把 offset 写成了 offfse
      const roleResult = await getPageListData('/role/list', {
        offset: 0,
        size: 1000
      })
      const { list: roleList } = roleResult.data
      // 获取初始的全部菜单数据
      const menuResult = await getPageListData('/menu/list', {})
      const { list: menuList } = menuResult.data

      // 保存数据
      this.entireDepartment = departmentList
      this.entireRole = roleList
      this.entireMenu = menuList
    }
  }
})
